/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.user.rest;


import cn.hutool.core.util.ObjectUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.api.YshopException;
import co.yixiang.common.aop.NoRepeatSubmit;
import co.yixiang.common.bean.LocalUser;
import co.yixiang.common.interceptor.AuthCheck;
import co.yixiang.modules.user.domain.YxAuthentication;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxAuthenticationService;
import co.yixiang.modules.user.service.dto.AppLoginRequest;
import co.yixiang.modules.user.service.dto.YxAuthenticationDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户控制器
 * </p>
 *
 * @author hupeng
 * @since 2019-10-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "个人认证", tags = "用户:个人认证", description = "个人认证")
public class YxAuthenticationController {

    private final YxAuthenticationService yxAuthenticationService;

    /**
     * 个人认证
     * @return
     */
    @AuthCheck
    @PostMapping("/authentication/applyAuthentication")
    @NoRepeatSubmit
    @ApiOperation(value = "个人认证申请",notes = "个人认证申请")
    public ApiResult<Object> applyAuthentication(@Validated @RequestBody YxAuthenticationDto yxAuthentication){
        YxUser yxUser = LocalUser.getUser();
        yxAuthentication.setUid(yxUser.getUid());
        return yxAuthenticationService.applyAuthentication(yxAuthentication);
    }

    /**
     * 个人认证详情
     * @return
     */
    @AuthCheck
    @GetMapping("/authentication/applyAuthenticationDetail")
    @ApiOperation(value = "个人认证申请详情",notes = "个人认证申请详情")
    public ApiResult<Object> applyAuthentication(){
        YxUser yxUser = LocalUser.getUser();

        YxAuthentication authentication = yxAuthenticationService.getOne(new QueryWrapper<YxAuthentication>().lambda().eq(YxAuthentication::getUid, yxUser.getUid()));

        if(ObjectUtil.isEmpty(authentication)){
            throw new YshopException("当前用户未申请实名认证");
        }
        return ApiResult.ok(authentication);
    }

}

